package com.zzy.service.impl;

import com.zzy.dao.BaseDao;
import com.zzy.model.*;
import com.zzy.service.FlowService;
import com.zzy.service.LeaveService;
import com.zzy.util.activiti.UtilFlow;
import org.hibernate.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户接口的实现
 * @author zzy
 *
 */
@Transactional
@Service()
//springMVC和hibernate结合必须加上不然会报
//No Session found for current thread 异常
public class LeaveServiceImpl implements LeaveService {

    @Autowired
    private BaseDao<Leave> basedao;
    @Autowired
    private FlowService flowService;


    public List<Leave> getAll(String hql,Object param[]) {
        return basedao.find(hql,param );
    }

    public Leave getUnique(String hql, Object[] param) {
        return basedao.get(hql,param);
    }


    public Leave getByID(String  id) {
        return  basedao.get(Leave.class,id);
    }
    public Integer getTotalNum( String hql ,Object[] param) {
        long k = basedao.count(hql,param);
        return (int) k;
    }
    public List<Leave> getPage(String hql, Object[] param, Integer page, Integer rows) {
        return basedao.find(hql, param, page, rows);
    }

    public List<Leave> getPage(Criteria cr, Integer page, Integer rows) {
        return basedao.find(cr,page,rows);
    }

    public Integer del(String id) {
        int i = 1;
        try {
            if (StringUtils.hasText(id)){
               for(String did : id.split(",")){
                   Leave o = new Leave();
                   o=this.getByID(did);
                   basedao.delete(o);
               }
            }
        } catch (Exception e) {
            i = -1;
        }
        return i;
    }
    public Integer saveOrUpdate(Leave model) {
        int i = 1;
        try {
            basedao.saveOrUpdate(model);
        } catch (Exception e) {
            i =  -1;
        }
        return i;
    }
    public Integer save(Leave model) {
        int i = 1;
        try {
            basedao.save(model);
        } catch (Exception e) {
            i =  -1;
        }
        return i;
    }



    public Integer update(Leave model) {
        int i = 1;
        try {
            basedao.update(model);
        } catch (Exception e) {
            i =  -1;
            e.printStackTrace();
        }
        return i;
    }

    public List<Leave> getList(String hql, Object[] param) {
        return basedao.find(hql, param);
    }

    public List<Leave> getList(Criteria cr) {
        return basedao.find(cr);
    }




    public void audit(CompleteParams params, MultipartFile[] files) {

        Leave model = getByID(params.getStiId());
        String nodeId = model.getNodeId();//本节点的 的节点id

        //初审 分配专家审核
        if(nodeId.contains("02")){//特殊节点处理

        }

        String fileUrl = "";
        if(files!=null && files.length>0){
            //fileUrl = fileService.upload(files); //删除文件
            params.setLinkurl(fileUrl);
        }

       /* if(nodeId.contains("TJ")){//提交申请,这个暂时没用
            params.setType(ActEnumType.COMMIT);
        }else{
            params.setType(ActEnumType.AUDIT);//审核
        }*/

        // 完成任务
        flowService.saveComplete(params);
        //更新 评奖管理的 流程节点名字和节点ID 发送消息
        TaskInfo taskInfo = flowService.getCurrentTask(params.getProcInsId());
        String definitionKey = "";
        String taskName = "";
        if(taskInfo != null){
            definitionKey = taskInfo.getDefinitionKey();
            taskName = taskInfo.getTaskName();
        }


        model.setNodeId(definitionKey);
        model.setNodeName(taskName);

        String approve = params.getApprove();
        if(approve.equals("y")){//通过
            if( nodeId.contains("JS")){//节点 结束
                model.setState("9");//9 项目结束
            }else{
                model.setState("1");//不管上次是不是1这次都更新，减少判断，表示项目进行中
            }
        }else if(approve.equals("n")){//退回
            model.setState("2");//2 代表项目被驳回
        }

        basedao.update(model);


    }



}
